<template>
    <ElTable :data="tableData" class="my-table" @select="select" @select-all="selectAll">
        <ElTableColumn type="selection" width="55"></ElTableColumn>
        <ElTableColumn label="文件ID" width="180" prop="fileId"></ElTableColumn>
        <ElTableColumn label="名称" width="240" prop="name"></ElTableColumn>
        <ElTableColumn label="描述" prop="description"></ElTableColumn>
        <ElTableColumn label="注册时间" prop="createTime" width="170"></ElTableColumn>
        <ElTableColumn label="操作" width="120">
            <template #default="{ row }">
                <ElButton type="danger" link @click="deleteRow(row.id)">删除</ElButton>
                <ElButton type="primary" link @click="download(row.fileId)">下载</ElButton>
            </template>
        </ElTableColumn>
    </ElTable>
</template>

<script setup lang="ts">
import { ElMessageBox, ElMessage } from 'element-plus';

interface Params {
    searchKey: string;
    current: number;
    size: number;
}

const emits = defineEmits<{
    (event: 'select', value: { isAll: boolean; count: number; ids: string[] }): void;
}>();

const tableData = ref<any[]>([]);

const params = ref<Params>({
    searchKey: '',
    current: 1,
    size: 10,
});

const getData = async (data?: Partial<Params>): Promise<number> => {
    if (data) {
        params.value = { ...params.value, ...data };
    }
    const res = await MyService.product.getProfileList(params.value);
    if (res.code === 200) {
        tableData.value = res.data.records;
        emits('select', { isAll: false, count: 0, ids: [] });
        return res.data.total;
    } else {
        return 0;
    }
};

const download = async (fileId: string) => {
    const res = await MyService.product.getProfileDownloadUrl({ fileId });
    if (res.code === 200 && res.data) {
        downloadFileByUrl(res.data);
    }
};

const downloadFileByUrl = (url: string) => {
    const downloadLink = document.createElement('a');
    downloadLink.href = url;
    downloadLink.click();
    URL.revokeObjectURL(url);
};

const deleteRow = async (fileId: string) => {
    try {
        await ElMessageBox.confirm('是否确认删除改资料库？', '操作提示', {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
            type: 'warning',
        });
        const res = await MyService.product.deleteProfile({ ids: [fileId] });
        if (res.code) {
            getData({ current: 1 });
        } else {
            ElMessage.error({ message: '资料库删除失败' });
        }
    } catch (error) {}
};

const select = (selection: any[], _row: any) => {
    const isAll = selection.length > 0 && selection.length === tableData.value.length;
    const count = selection.length;
    const ids = selection.map((item) => item.id);
    emits('select', { isAll, count, ids });
};

const selectAll = (selection: any[]) => {
    const isAll = selection.length > 0 && selection.length === tableData.value.length;
    const count = selection.length;
    const ids = selection.map((item) => item.id);
    emits('select', { isAll, count, ids });
};

defineExpose({
    refresh: getData,
});
</script>

<style scoped></style>
